package com.project.webapp.action.backend.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.project.dao.bean.Menu;
import com.project.service.backend.basic.IMenuService;
import com.project.webapp.action.BaseAction;
import com.project.webapp.form.backend.basic.MenuForm;

/**
 * 後台 菜單信息管理 Action
 * @author Sn
 *
 */
public class MenuAction extends BaseAction {

    private static final String ADD_MENU = "addMenu";
    //操作结果：suc/fail
    private String operateResult = "";
    private MenuForm menuForm;
    private IMenuService menuService;
    List<Menu> menuList;
    //菜单树的json对象
    private JSONArray jsonArray;
    
    /**
     * <pre>
     * 菜单信息管理
     * </pre>
     */
    private static final long serialVersionUID = 1L;

    /**
     * 进入菜单信息管理画面
     * @return
     * @throws Exception
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    @Override
    public String execute() 
    {
        menuForm = new MenuForm();
        if (menuList != null && menuList.size() > 0) 
        {
            menuList.clear();
        }
        return this.query();
    }

    /**
     * 
     * <pre>
     * 显示新增画面
     * </pre>
     *
     * @return
     */
    public String showAdd() 
    {
        menuForm = new MenuForm();
        operateResult = "";
        this.getTree();
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 查询
     * </pre>
     *
     * @return
     */
    public String query() 
    {
        try 
        {
            operateResult = "";
            menuList = menuService.findMenuByPg(menuForm);
            
        } catch (Exception e) 
        {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 删除
     * </pre>
     *
     * @return
     */
    public String delete() 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        String menuId = req.getParameter("menuId");
        try 
        {
            menuForm.setMenuId(menuId);         
            //判断该节点下是否还存在子节点
            if(menuService.isHaveSubMenu(menuForm)) 
            {
                operateResult = "haveSub";
                menuList = menuService.findMenuByPg(menuForm);
                return SUCCESS;
            }
            menuService.delete(menuForm);
            menuList = menuService.findMenuByPg(menuForm);
            operateResult = "suc";
            
        } catch (Exception e) 
        {
            e.printStackTrace();
            operateResult = "fail";
        }
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 编辑
     * </pre>
     *
     * @return
     */
    public String edit() 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        if (menuForm == null) 
        {
            menuForm = new MenuForm();
        }
        
        this.getTree();
        operateResult = "";
        String menuName = req.getParameter("menuName");
        System.out.println("菜单名称："+menuName);
        String menuSeq = req.getParameter("menuSeq");
        String menuId = req.getParameter("menuId");
        String parentId = req.getParameter("parentId");
        String menuCode = req.getParameter("menuCode");
        String menuSort = req.getParameter("menuSort");
        menuForm.setMenuName(menuName);
        if (menuSeq != null && !"".equals(menuSeq)) 
        {
            menuForm.setMenuSeq(new Integer(menuSeq));
        } else 
        {
            menuForm.setMenuSeq(new Integer("0"));
        }
        
        menuForm.setMenuId(menuId);
        menuForm.setParentId(parentId);
        menuForm.setMenuCode(menuCode);
        menuForm.setMenuSort(new Integer(menuSort));

        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 保存
     * </pre>
     *
     * @return
     */
    public String save() 
    {
        try 
        {
            menuService.save(menuForm);
            operateResult = "suc";
            
        } catch (Exception e) 
        {
            e.printStackTrace();
            operateResult = "fail";
        }
        menuForm = new MenuForm();
        return ADD_MENU;
    }

    public void getTree() 
    {
        try 
        {
            List<Menu> menuList = null;
            List<Map<String, String>> jsonDataSourceList = new ArrayList<Map<String, String>>();
            menuList = menuService.findMenuForTree();
            if (menuList != null && menuList.size() > 0) 
            {
                for (Menu m : menuList) 
                {
                    Map<String, String> menuVoMap = new HashMap<String, String>();
                    menuVoMap.put("id", m.getMenuId());
                    menuVoMap.put(
                        "pId",
                        (m.getParentId() == null || "".equals(m.getParentId())) ? "1" : m
                            .getParentId());
                    menuVoMap.put("name", m.getMenuName());
                    menuVoMap.put("open", "false");
                    jsonDataSourceList.add(menuVoMap);
                }
            }
            jsonArray = JSONArray.fromObject(jsonDataSourceList);
            JSONObject json = new JSONObject();
            json.put("id", 1);
            json.put("name", "根菜单");
            json.put("pId", 0);
            json.put("isParent", true);
            json.put("open", true);
            jsonArray.add(json);
            
        } catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public String getOperateResult() {
        return operateResult;
    }

    public void setOperateResult(String operateResult) {
        this.operateResult = operateResult;
    }

    public MenuForm getMenuForm() {
        return menuForm;
    }

    public void setMenuForm(MenuForm menuForm) {
        this.menuForm = menuForm;
    }

    public void setMenuService(IMenuService menuService) {
        this.menuService = menuService;
    }

    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    public JSONArray getJsonArray() {
        return jsonArray;
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }

}
